package com.zhongen.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.zhongen.admin.api.dto.UserDTO;
import com.zhongen.admin.api.dto.UserInfo;
import com.zhongen.admin.api.entity.*;
import com.zhongen.admin.api.util.HttpClientUtil;
import com.zhongen.admin.api.util.ParamResolver;
import com.zhongen.admin.api.vo.UserExcelVO;
import com.zhongen.admin.api.vo.UserVO;
import com.zhongen.admin.mapper.SysUserDeptMapper;
import com.zhongen.admin.mapper.SysUserMapper;
import com.zhongen.admin.mapper.SysUserPostMapper;
import com.zhongen.admin.mapper.SysUserRoleMapper;
import com.zhongen.admin.service.*;
import com.zhongen.common.core.constant.CacheConstants;
import com.zhongen.common.core.constant.CommonConstants;
import com.zhongen.common.core.exception.ErrorCodes;
import com.zhongen.common.core.util.MsgUtils;
import com.zhongen.common.core.util.R;
import com.zhongen.common.security.util.SecurityUtils;
import com.zhongen.quartz.util.WxProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

	private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

	private final SysMenuService sysMenuService;

	private final SysRoleService sysRoleService;

	private final SysPostService sysPostService;

	private final SysDeptService sysDeptService;

	private final SysUserRoleMapper sysUserRoleMapper;

	private final SysUserPostMapper sysUserPostMapper;

	private final SysUserDeptMapper sysUserDeptMapper;

	private final CacheManager cacheManager;

	private final WxProperties wxProperties;

	/**
	 * 保存用户信息
	 * @param userDto DTO 对象
	 * @return success/fail
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveUser(UserDTO userDto) {
		SysUser sysUser = new SysUser();
		BeanUtils.copyProperties(userDto, sysUser);
		sysUser.setDelFlag(CommonConstants.STATUS_NORMAL);
		sysUser.setCreateBy(userDto.getUsername());
		sysUser.setPassword(ENCODER.encode(userDto.getPassword()));
		sysUser.setPinyin(PinyinUtil.getPinyin(userDto.getName()));
		sysUser.setPy(PinyinUtil.getFirstLetter(userDto.getName(), " "));
		baseMapper.insert(sysUser);
		// 保存用户部门信息
		Optional.ofNullable(userDto.getDeptIds()).ifPresent(deptIds -> {
			deptIds.stream().map(deptId -> {
				SysUserDept userDept = new SysUserDept();
				userDept.setUserId(sysUser.getUserId());
				userDept.setDeptId(deptId);
				return userDept;
			}).forEach(sysUserDeptMapper::insert);
		});
		// 保存用户岗位信息
		Optional.ofNullable(userDto.getPost()).ifPresent(posts -> {
			posts.stream().map(postId -> {
				SysUserPost userPost = new SysUserPost();
				userPost.setUserId(sysUser.getUserId());
				userPost.setPostId(postId);
				return userPost;
			}).forEach(sysUserPostMapper::insert);
		});

		// 如果角色为空，赋默认角色
		if (CollUtil.isEmpty(userDto.getRole())) {
			// 获取默认角色编码
			String defaultRole = ParamResolver.getStr("USER_DEFAULT_ROLE");
			// 默认角色
			SysRole sysRole = sysRoleService
				.getOne(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleCode, defaultRole));
			userDto.setRole(Collections.singletonList(sysRole.getRoleId()));
		}

		// 插入用户角色关系表
		userDto.getRole().stream().map(roleId -> {
			SysUserRole userRole = new SysUserRole();
			userRole.setUserId(sysUser.getUserId());
			userRole.setRoleId(roleId);
			return userRole;
		}).forEach(sysUserRoleMapper::insert);
		return Boolean.TRUE;
	}

	/**
	 * 通过查用户的全部信息
	 * @param sysUser 用户
	 * @return
	 */
	@Override
	public UserInfo findUserInfo(SysUser sysUser) {
		UserInfo userInfo = new UserInfo();
		userInfo.setSysUser(sysUser);

		// 设置角色列表 （ID）
		List<SysRole> roles = sysRoleService.findRolesByUserId(sysUser.getUserId());
		userInfo.setRoleList(roles);
		List<Long> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
		userInfo.setRoles(ArrayUtil.toArray(roleIds, Long.class));

		// 设置权限列表（menu.permission）
		Set<String> permissions = new HashSet<>();
		roleIds.forEach(roleId -> {
			List<String> permissionList = sysMenuService.findMenuByRoleId(roleId)
				.stream()
				.map(SysMenu::getPermission)
				.filter(permission -> StrUtil.isNotEmpty(permission))
				.toList();
			permissions.addAll(permissionList);
		});
		userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class));
		return userInfo;
	}

	/**
	 * 分页查询用户信息（含有角色信息）
	 * @param page 分页对象
	 * @param userDTO 参数列表
	 * @return
	 */
	@Override
	public IPage getUsersWithRolePage(Page page, UserDTO userDTO) {
		return baseMapper.getUserVosPage(page, userDTO);
	}

	/**
	 * 通过ID查询用户信息
	 * @param id 用户ID
	 * @return 用户信息
	 */
	@Override
	public UserVO selectUserVoById(Long id) {
		return baseMapper.getUserVoById(id);
	}

	/**
	 * 删除用户
	 * @param userDTO
	 * @return Boolean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean deleteUserByIds(UserDTO userDTO) {
		List<Long> ids = userDTO.getIds();
		// 删除 spring cache
		List<SysUser> userList = baseMapper.selectBatchIds(ids);
		Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
		for (SysUser sysUser : userList) {
			// 立即删除
			cache.evictIfPresent(sysUser.getUsername());
		}

		// 用户-部门 一对多，不能直接删关联关系表
		if (ObjectUtils.isNotEmpty(userDTO.getDeptId())) {
			Long deptId = userDTO.getDeptId();
			for (Long id : ids) {
				List<SysUserDept> sysUserDepts = sysUserDeptMapper
					.selectList(Wrappers.<SysUserDept>lambdaQuery().eq(SysUserDept::getUserId, id));
				if (ObjectUtils.isNotEmpty(sysUserDepts)) {
					// 一对一
					if (sysUserDepts.size() == 1) {
						sysUserRoleMapper.delete(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, id));
						// 删除用户部门关系表
						sysUserDeptMapper.delete(Wrappers.<SysUserDept>lambdaQuery().in(SysUserDept::getUserId, id));
						this.removeBatchByIds(CollUtil.toList(ids));
					}
					else {
						// 一对多
						// 删除用户部门关系表
						sysUserDeptMapper.delete(Wrappers.<SysUserDept>lambdaQuery()
							.eq(SysUserDept::getDeptId, deptId)
							.in(SysUserDept::getUserId, id));
					}
				}
			}
		}
		else {
			sysUserRoleMapper.delete(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, ids));
			// 删除用户部门关系表
			sysUserDeptMapper.delete(Wrappers.<SysUserDept>lambdaQuery().in(SysUserDept::getUserId, ids));
			this.removeBatchByIds(CollUtil.toList(ids));
		}
		return Boolean.TRUE;
	}

	@Override
	@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#userDto.username")
	public R<Boolean> updateUserInfo(UserDTO userDto) {
		SysUser user = baseMapper
			.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, userDto.getUsername()));
		SysUser sysUser = new SysUser();
		sysUser.setPhone(userDto.getPhone());
		sysUser.setUserId(user.getUserId());
		sysUser.setAvatar(userDto.getAvatar());
		sysUser.setNickname(userDto.getNickname());
		sysUser.setName(userDto.getName());
		sysUser.setEmail(userDto.getEmail());
		return R.ok(this.updateById(sysUser));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#userDto.username")
	public Boolean updateUser(UserDTO userDto) {
		// 更新用户表信息
		SysUser sysUser = new SysUser();
		BeanUtils.copyProperties(userDto, sysUser);
		sysUser.setUpdateTime(LocalDateTime.now());
		if (StrUtil.isNotBlank(userDto.getPassword())) {
			sysUser.setPassword(ENCODER.encode(userDto.getPassword()));
		}
		if (ObjectUtils.isNotEmpty(userDto.getName())) {
			sysUser.setPinyin(PinyinUtil.getPinyin(userDto.getName()));
			sysUser.setPy(PinyinUtil.getFirstLetter(userDto.getName(), " "));
		}
		this.updateById(sysUser);

		// 更新用户角色表
		sysUserRoleMapper.delete(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, userDto.getUserId()));
		userDto.getRole().stream().map(roleId -> {
			SysUserRole userRole = new SysUserRole();
			userRole.setUserId(sysUser.getUserId());
			userRole.setRoleId(roleId);
			return userRole;
		}).forEach(SysUserRole::insert);

		// 更新用户岗位表
		sysUserPostMapper.delete(Wrappers.<SysUserPost>lambdaQuery().eq(SysUserPost::getUserId, userDto.getUserId()));
		userDto.getPost().stream().map(postId -> {
			SysUserPost userPost = new SysUserPost();
			userPost.setUserId(sysUser.getUserId());
			userPost.setPostId(postId);
			return userPost;
		}).forEach(SysUserPost::insert);

		// 更新用户部门表
		sysUserDeptMapper.delete(Wrappers.<SysUserDept>lambdaQuery().eq(SysUserDept::getUserId, userDto.getUserId()));
		userDto.getDeptIds().stream().map(deptId -> {
			SysUserDept userDept = new SysUserDept();
			userDept.setUserId(sysUser.getUserId());
			userDept.setDeptId(deptId);
			return userDept;
		}).forEach(SysUserDept::insert);
		return Boolean.TRUE;
	}

	/**
	 * 查询全部的用户
	 * @param userDTO 查询条件
	 * @return list
	 */
	@Override
	public List<UserExcelVO> listUser(UserDTO userDTO) {
		// 根据数据权限查询全部的用户信息
		List<UserVO> voList = baseMapper.selectVoList(userDTO);
		// 转换成execl 对象输出
		List<UserExcelVO> userExcelVOList = voList.stream().map(userVO -> {
			UserExcelVO excelVO = new UserExcelVO();
			BeanUtils.copyProperties(userVO, excelVO);
			String roleNameList = userVO.getRoleList()
				.stream()
				.map(SysRole::getRoleName)
				.collect(Collectors.joining(StrUtil.COMMA));
			excelVO.setRoleNameList(roleNameList);
			String postNameList = userVO.getPostList()
				.stream()
				.map(SysPost::getPostName)
				.collect(Collectors.joining(StrUtil.COMMA));
			excelVO.setPostNameList(postNameList);
			return excelVO;
		}).collect(Collectors.toList());
		return userExcelVOList;
	}

	/**
	 * excel 导入用户, 插入正确的 错误的提示行号
	 * @param excelVOList excel 列表数据
	 * @param bindingResult 错误数据
	 * @return ok fail
	 */
	@Override
	public R importUser(List<UserExcelVO> excelVOList, BindingResult bindingResult) {
		// 通用校验获取失败的数据
		List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
		List<SysDept> deptList = sysDeptService.list();
		List<SysRole> roleList = sysRoleService.list();
		List<SysPost> postList = sysPostService.list();

		// 执行数据插入操作 组装 UserDto
		for (UserExcelVO excel : excelVOList) {
			// 个性化校验逻辑
			List<SysUser> userList = this.list();

			Set<String> errorMsg = new HashSet<>();
			// 校验用户名是否存在
			boolean exsitUserName = userList.stream()
				.anyMatch(sysUser -> excel.getUsername().equals(sysUser.getUsername()));

			if (exsitUserName) {
				errorMsg.add(MsgUtils.getMessage(ErrorCodes.SYS_USER_USERNAME_EXISTING, excel.getUsername()));
			}

			// 判断输入的部门名称列表是否合法
			Optional<SysDept> deptOptional = deptList.stream()
				.filter(dept -> excel.getDeptName().equals(dept.getName()))
				.findFirst();
			if (!deptOptional.isPresent()) {
				errorMsg.add(MsgUtils.getMessage(ErrorCodes.SYS_DEPT_DEPTNAME_INEXISTENCE, excel.getDeptName()));
			}

			// 判断输入的角色名称列表是否合法
			List<String> roleNameList = StrUtil.split(excel.getRoleNameList(), StrUtil.COMMA);
			List<SysRole> roleCollList = roleList.stream()
				.filter(role -> roleNameList.stream().anyMatch(name -> role.getRoleName().equals(name)))
				.collect(Collectors.toList());

			if (roleCollList.size() != roleNameList.size()) {
				errorMsg.add(MsgUtils.getMessage(ErrorCodes.SYS_ROLE_ROLENAME_INEXISTENCE, excel.getRoleNameList()));
			}

			// 判断输入的部门名称列表是否合法
			List<String> postNameList = StrUtil.split(excel.getPostNameList(), StrUtil.COMMA);
			List<SysPost> postCollList = postList.stream()
				.filter(post -> postNameList.stream().anyMatch(name -> post.getPostName().equals(name)))
				.collect(Collectors.toList());

			if (postCollList.size() != postNameList.size()) {
				errorMsg.add(MsgUtils.getMessage(ErrorCodes.SYS_POST_POSTNAME_INEXISTENCE, excel.getPostNameList()));
			}

			// 数据合法情况
			if (CollUtil.isEmpty(errorMsg)) {
				insertExcelUser(excel, deptOptional, roleCollList, postCollList);
			}
			else {
				// 数据不合法情况
				errorMessageList.add(new ErrorMessage(excel.getLineNum(), errorMsg));
			}

		}

		if (CollUtil.isNotEmpty(errorMessageList)) {
			return R.failed(errorMessageList);
		}
		return R.ok();
	}

	/**
	 * 插入excel User
	 */
	private void insertExcelUser(UserExcelVO excel, Optional<SysDept> deptOptional, List<SysRole> roleCollList,
			List<SysPost> postCollList) {
		UserDTO userDTO = new UserDTO();
		userDTO.setUsername(excel.getUsername());
		userDTO.setPhone(excel.getPhone());
		userDTO.setNickname(excel.getNickname());
		userDTO.setName(excel.getName());
		userDTO.setEmail(excel.getEmail());
		// 批量导入初始密码为手机号
		userDTO.setPassword(userDTO.getPhone());
		// 根据部门名称查询部门ID
		userDTO.setDeptId(deptOptional.get().getDeptId());
		// 插入岗位名称
		List<Long> postIdList = postCollList.stream().map(SysPost::getPostId).collect(Collectors.toList());
		userDTO.setPost(postIdList);
		// 根据角色名称查询角色ID
		List<Long> roleIdList = roleCollList.stream().map(SysRole::getRoleId).collect(Collectors.toList());
		userDTO.setRole(roleIdList);
		// 插入用户
		this.saveUser(userDTO);
	}

	/**
	 * 注册用户 赋予用户默认角色
	 * @param userDto 用户信息
	 * @return success/false
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R<Boolean> registerUser(UserDTO userDto) {
		// 判断用户名是否存在
		SysUser sysUser = this.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, userDto.getUsername()));
		if (sysUser != null) {
			String message = MsgUtils.getMessage(ErrorCodes.SYS_USER_USERNAME_EXISTING, userDto.getUsername());
			return R.failed(message);
		}
		return R.ok(saveUser(userDto));
	}

	/**
	 * 锁定用户
	 * @param username 用户名
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#username")
	public R<Boolean> lockUser(String username) {
		SysUser sysUser = baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));

		if (Objects.nonNull(sysUser)) {
			sysUser.setLockFlag(CommonConstants.STATUS_LOCK);
			baseMapper.updateById(sysUser);
		}
		return R.ok();
	}

	@Override
	@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#userDto.username")
	public R changePassword(UserDTO userDto) {
		UserVO userVO = baseMapper.getUserVoByUsername(userDto.getUsername());
		if (Objects.isNull(userVO)) {
			return R.failed("用户不存在");
		}

		if (StrUtil.isEmpty(userDto.getPassword())) {
			return R.failed("原密码不能为空");
		}

		if (!ENCODER.matches(userDto.getPassword(), userVO.getPassword())) {
			log.info("原密码错误，修改个人信息失败:{}", userDto.getUsername());
			return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_UPDATE_PASSWORDERROR));
		}

		if (StrUtil.isEmpty(userDto.getNewpassword1())) {
			return R.failed("新密码不能为空");
		}
		String password = ENCODER.encode(userDto.getNewpassword1());

		int pwdChangeNum = userVO.getPwdChangeNum() + 1;
		this.update(Wrappers.<SysUser>lambdaUpdate()
			.set(SysUser::getPassword, password)
			.set(SysUser::getPwdChangeNum, pwdChangeNum)
			.eq(SysUser::getUserId, userVO.getUserId()));
		return R.ok();
	}

	@Override
	public R checkPassword(String password) {
		String username = SecurityUtils.getUser().getUsername();
		SysUser condition = new SysUser();
		condition.setUsername(username);
		SysUser sysUser = this.getOne(new QueryWrapper<>(condition));

		if (!ENCODER.matches(password, sysUser.getPassword())) {
			log.info("原密码错误");
			return R.failed("密码输入错误");
		}
		else {
			return R.ok();
		}
	}

	@Override
	public Boolean innerUpdateUser(UserDTO userDto) {
		// 更新用户表信息
		SysUser sysUser = new SysUser();
		BeanUtils.copyProperties(userDto, sysUser);
		sysUser.setUpdateTime(LocalDateTime.now());
		if (StrUtil.isNotBlank(userDto.getPassword())) {
			sysUser.setPassword(ENCODER.encode(userDto.getPassword()));
		}
		UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("nickname", sysUser.getNickname())
			.set("username", sysUser.getUsername())
			.set(ObjectUtils.isNotEmpty(sysUser.getEntryDate()), "entry_date", sysUser.getEntryDate())
			.eq("phone", sysUser.getPhone());
		baseMapper.update(updateWrapper);
		SysUser user = baseMapper.selectOne(new QueryWrapper<SysUser>().eq("phone", sysUser.getPhone()));
		// 更新用户部门信息
		Optional.ofNullable(userDto.getDeptIds()).ifPresent(deptIds -> {
			// 删除用户部门关联
			sysUserDeptMapper.delete(new QueryWrapper<SysUserDept>().eq("user_id", user.getUserId()));
			// 重新插入
			deptIds.stream().map(deptId -> {
				SysUserDept userDept = new SysUserDept();
				userDept.setUserId(user.getUserId());
				userDept.setDeptId(deptId);
				return userDept;
			}).forEach(sysUserDeptMapper::insert);
		});
		return true;
	}

	@Override
	public SysUser innerQueryUserByCondition(String condition, String value) {
		return baseMapper.selectOne(new QueryWrapper<SysUser>().eq(condition, value));
	}

	@Override
	public List<SysUser> allUserList(String username) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>().gt("dept_id", 10).eq("del_flag", 0);
		wrapper.select("username", "name", "phone", "user_id", "avatar");
		if (null != username && !"".equals(username)) {
			wrapper.like("username", username);
		}
		return baseMapper.selectList(wrapper);
	}

	/**
	 * 查询本地有，钉钉没有的用户，然后将本地改成已离职
	 * @param resignationInfo
	 * @return
	 */
	@Override
	public void updateByUsernames(List<SysUser> resignationInfo) {
		if (ObjectUtils.isNotEmpty(resignationInfo)) {
			baseMapper.updateByUsernames(resignationInfo);
		}
	}

	/**
	 * 修改次数默认为0，没改动一次+1，0~5次以内，每次登录成功都提示一次修改密码 大于5次不提示 勾选不再提示，直接改为10次
	 * @return
	 */
	@Override
	public boolean changePwdNum() {
		boolean flag = false;
		// 管理员账号忽略
		if (ObjectUtils.isNotEmpty(SecurityUtils.getUser().getUsername())
				&& "admin".equals(SecurityUtils.getUser().getUsername())) {
			flag = true;
		}
		SysUser sysUser = baseMapper
			.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserId, SecurityUtils.getUser().getId()));
		if (ObjectUtils.isNotEmpty(sysUser)) {
			int pwdChangeNum = sysUser.getPwdChangeNum();
			flag = pwdChangeNum > 5;
			if (!flag) {
				sysUser.setPwdChangeNum(pwdChangeNum + 1);
				baseMapper.updateById(sysUser);
			}
		}
		return flag;
	}

	/**
	 * 修改密码不再提示
	 * @return
	 */
	@Override
	public R noPrompt() {
		this.update(Wrappers.<SysUser>lambdaUpdate()
			.set(SysUser::getPwdChangeNum, 10)
			.eq(SysUser::getUserId, SecurityUtils.getUser().getId()));
		return R.ok();
	}

	@Override
	public R getInfoByCode(WxUserInfo wxUserInfo) {
		if (!StringUtils.isNotBlank(wxUserInfo.getCode())) {
			return R.ok("未获取到用户凭证code");
		}
		if (!StringUtils.isNotBlank(wxUserInfo.getPhone())) {
			return R.ok("未获取到用户手机号");
		}
		String appid = wxProperties.getAppid();
		String appSecret = wxProperties.getSecret();
		//cc0ddcade9d2d8a200db9d36db0f2a98
		String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + appSecret + "&js_code=" + wxUserInfo.getCode() + "&grant_type=authorization_code";
		String responseBody = HttpClientUtil.doGet(apiUrl);
		JSONObject jsonObject = JSON.parseObject(responseBody);
		if(!jsonObject.containsKey("unionID")){

		}
		if(!jsonObject.containsKey("openid")){
			return R.ok(jsonObject);
		}
		String openid = jsonObject.getString("openid");
//		//解密获取用户信息
//		JSONObject result = WechatGetUserInfoUtil.getUserInfo(wxUserInfo.getEncryptedData(), jsonObject.getString("session_key"), wxUserInfo.getIv());
//		result.put("openid", jsonObject.getString("openid"));
		SysUser sysUser = baseMapper.getUserVoByPhone(wxUserInfo.getPhone());
		if(sysUser!=null){
			baseMapper.updateOpenid(wxUserInfo.getPhone(), openid);
		}else {
			SysUser newUser = new SysUser();
			newUser.setPhone(wxUserInfo.getPhone());
			newUser.setUsername(wxUserInfo.getPhone());
			newUser.setName("游客");
			newUser.setWxOpenid(openid);
			newUser.setDelFlag("0");
			newUser.setCreateTime(LocalDate.now().atStartOfDay());
			newUser.setCreateBy("游客");
			baseMapper.insert(newUser);
		}
		return R.ok("保存成功");
	}

}
